package pip

type defaultFilterHandler struct {
	FilterList      []ElementFilter
	PluginContainer FilterPluginContainer
}

func Filter_FilterChildren(ctx *FilterContext, ele ElementType) (bool, FilterType) {
	if ele.Age < 18 {
		return true, FilterChildren
	}
	return false, FilterChildren
}

func chainFilters(filters ...ElementFilter) (list []ElementFilter) {
	for i := range filters {
		list = append(list, filters[i])
	}
	return
}

func (h *defaultFilterHandler) appendElementFilters(filters ...ElementFilter) *defaultFilterHandler {
	for i := range filters {
		h.FilterList = append(h.FilterList, filters[i])
	}
	return h
}

func (h *defaultFilterHandler) Filter(ctx *FilterContext, candidateList []ElementType) (result []ElementType, filterResult *FilterResult, err error) {
	if h.PluginContainer != nil {
		h.PluginContainer.preFilter(ctx, candidateList)
	}
	filterResult = NewFilterResult()
	for i := range candidateList {
		ele := candidateList[i]
		for j := range h.FilterList {
			eFilter := h.FilterList[j]
			isFiltered, filterType := eFilter(ctx, ele)
			if isFiltered {
				filterResult.append(ele, filterType)
			} else {
				result = append(result, ele)
			}
		}
	}
	if h.PluginContainer != nil {
		h.PluginContainer.postFilter(ctx, candidateList, result, filterResult, err)
	}
	return
}
